<?php

// $Id$

global $WSC;

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_attr_types/tcms_cia_text_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_attr_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/i_ci.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_deleter.class.php');


class TCMS_CI_Article_DBC extends Tangra_Class implements I_DB_Storable, I_CI {
	private $ci;
	private $title;
	private $intro;
	private $content;


	function __construct() {
		$this->title = new TCMS_CIA_Text_DBC();
		$this->content = new TCMS_CIA_Text_DBC();
		$this->intro = new TCMS_CIA_Text_DBC();
		$this->ci = new TCMS_CI_DBC();
	}


	public function save(DB_Connection $dbc, $current_user_id = 0) {
		$ret = false;


		$ci_type = $this->fetch_ci_type($dbc);

		if (!$this->ci->get_id()) {
			$this->ci->set_type($ci_type->get_id());

			$content_attr = $this->fetch_ci_type_attr($dbc, $ci_type->get_id(), 'content');
			$intro_attr = $this->fetch_ci_type_attr($dbc, $ci_type->get_id(), 'intro');

			$ci_id = $this->ci->save($dbc, $current_user_id);

			$this->title->set_ci($ci_id);
			$title_attr = $this->fetch_ci_type_attr($dbc, $ci_type->get_id(), 'title');
			$this->title->set_ci_type_attr($title_attr->get_id());
			$this->title->save($dbc);

			$this->content->set_ci($ci_id);
			$content_attr = $this->fetch_ci_type_attr($dbc, $ci_type->get_id(), 'content');
			$this->content->set_ci_type_attr($content_attr->get_id());
			$this->content->save($dbc);

			if ($this->intro->get_value()) {

				$this->intro->set_ci($ci_id);
				$intro_attr = $this->fetch_ci_type_attr($dbc, $ci_type->get_id(), 'intro');
				$this->intro->set_ci_type_attr($intro_attr->get_id());
				$this->intro->save($dbc);
			}

			$ret = true;
		} else {
			$ci_id = $this->ci->save($dbc, $current_user_id);
			$this->title->save($dbc);
			$this->content->save($dbc);
			if (!$this->intro->get_id()) {
				if ($this->intro->get_value()) {
					$this->intro->set_ci($ci_id);
					$intro_attr = $this->fetch_ci_type_attr($dbc, $ci_type->get_id(), 'intro');
					$this->intro->set_ci_type_attr($intro_attr->get_id());
					$this->intro->save($dbc);
				}
			} else {
				$this->intro->save($dbc);
			}

			$ret = $ci_id;
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		if ($this->ci->load_by_id($dbc, $id)) {
			if ($this->title->load_by_ci_thid($dbc, $id, 'title')) {
				if ($this->content->load_by_ci_thid($dbc, $id, 'content')) {
					$this->intro->load_by_ci_thid($dbc, $id, 'intro');

					$ret = $id;
				}
			}
		}

		return $ret;
	}


	private function fetch_ci_type(DB_Connection $dbc) {
		$ci_type = new TCMS_CI_Type_DBC();
		if (!$ci_type->load_by_hid($dbc, 'article')) {
			throw new Tangra_Module_Exception('Cannot find content type with hid "article"');
		}

		return $ci_type;
	}


	private function fetch_ci_type_attr(DB_Connection $dbc, $ci_type_id, $hid) {
		$cita = new TCMS_CI_Type_Attr_DBC();
		if (!$cita->load_by_type_and_hid($dbc, $ci_type_id, $hid)) {
			throw new Tangra_Module_Exception('Cannot find content type attribute. ci_type_id: '.$ci_type_id.' hid:'.$hid);
		}

		return $cita;
	}


	public function set_id($id) {
		$this->ci->set_id($id);
	}


	public function get_id() {
		return $this->ci->get_id();
	}


	public function set_ci(TCMS_CI_DBC $ci) {
		$this->ci = $ci;
	}

	public function get_ci() {
		return $this->ci;
	}


	public function set_title(TCMS_CIA_Text_DBC $title) {
		$this->title = $title;
	}

	public function get_title() {
		return $this->title;
	}


	public function set_content(TCMS_CIA_Text_DBC $content) {
		$this->content = $content;
	}

	public function get_content() {
		return $this->content;
	}


	public function set_intro(TCMS_CIA_Text_DBC $intro) {
		$this->intro = $intro;
	}


	public function get_intro() {
		return $this->intro;
	}


	public function get_as_short_plain_text() {
		return strip_tags($this->get_title()->get_value());
	}


	public function get_as_plain_text() {
		$ret = strip_tags($this->get_title()->get_value() ."\r\n" . $this->get_intro()->get_value() ."\r\n" . $this->get_content()->get_value());

		return $ret;
	}


	public static function delete_cascade(DB_Connection $dbc, $ci_id, $hidden_inc_dir, $current_user_id) {
		TCMS_CI_Deleter::delete($dbc, $ci_id, $hidden_inc_dir, $current_user_id);
	}
}